package com.xxx.car.web.admin;

import com.github.pagehelper.Page;
import com.xxx.car.entity.vo.request.ReqUserCarAdminQuery;
import com.xxx.car.entity.vo.response.RespUserCarAdmin;
import com.xxx.car.service.UserCarService;
import com.xxx.common.base.enums.AdminErrorEnum;
import com.xxx.common.base.vo.AdminResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by wenjiexu on 2017/6/24.
 */
@Controller
@RequestMapping("/admin/car")
public class AdminCarController {

    @Autowired
    private UserCarService userCarService;

    @RequestMapping("/")
    public String index() {
        return "car";
    }

    @RequestMapping("/list")
    @ResponseBody
    public AdminResponse query(@Valid @ModelAttribute ReqUserCarAdminQuery query, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        Page<RespUserCarAdmin> page = userCarService.queryForAdmin(query);
        return AdminResponse.returnSuccess(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    @RequestMapping("/get")
    @ResponseBody
    public AdminResponse query(@RequestParam Integer carId) {
        RespUserCarAdmin car = userCarService.getForAdmin(carId);
        return AdminResponse.returnSuccess(car);
    }

    @RequestMapping("/countInfo")
    @ResponseBody
    public AdminResponse count() {
        return AdminResponse.returnSuccess(userCarService.statusGroupCount());
    }

    @RequestMapping("/audit")
    @ResponseBody
    public AdminResponse audit(@RequestParam Integer carId, @RequestParam Boolean pass, String carErrMsg) {
        userCarService.audit(carId, pass, carErrMsg);
        return AdminResponse.returnSuccess();
    }

}
